<?php

function addItem($name)
{		
	$insert  = "INSERT INTO ".DB_PERFIX."template_images (`name`)"; 
	$insert .= "VALUES ('".$name."');";
	$result = mysql_query($insert);
	$result = mysql_query('SELECT id FROM '.DB_PERFIX.'template_images ORDER BY id DESC LIMIT 1;');
	while($row = mysql_fetch_array($result))
	{ $item['id'] = $row['id']; }
	return $item['id'];
}
	
function getItem($id)
{
	$result = mysql_query('SELECT * FROM '.DB_PERFIX.'template_images WHERE id='.$id.' LIMIT 1;');
	while($row = mysql_fetch_array($result))
	{
		$item['id'] = $row['id'];
		$item['name'] = $row['name'];
		$item['image'] = $row['image'];
		$item['style'] = $row['style'];
		$item['type'] = $row['type'];
		$item['active'] = $row['active'];
	}
	return $item;
}
function setItemName($id, $value)
{
	$update = 'UPDATE '.DB_PERFIX.'template_images SET name="'.$value.'" WHERE id='.$id.' LIMIT 1;';
	$update = mysql_query($update);
	return $update;
}
function setItemType($id, $value)
{
	$update = mysql_query('UPDATE '.DB_PERFIX.'template_images SET type='.$value.' WHERE id='.$id.' LIMIT 1;');
	return $update;
}
function setItemStyle($id, $value)
{
	$update = 'UPDATE '.DB_PERFIX.'template_images SET style="'.$value.'" WHERE id='.$id.' LIMIT 1;';
	$update = mysql_query($update);
	return $update;
}
function deleteItem($id)
{
	$result = mysql_query('DELETE FROM '.DB_PERFIX.'template_images WHERE id='.$id.';');
	return $result;
}
function uploadItemImage($id, $old_image)
{
	if (image_valid($_FILES['image']['type']) && ($_FILES['image']['name'] != ""))
	{
		if($_FILES["image"]["size"] < 20000)
		{
			$error['image'] = "image size too big:".$_FILES["image"]["size"];
		}
		$type = $_FILES['image']['type'];
		$temp_name = $_FILES["image"]["name"];
		$extension = substr($temp_name,-3);
		$new_name = $userid.md5(uniqid()).".".$extension;
		
		if (is_file(HTML_IMAGE_PATH.$old_image))
		{
			if (!unlink(HTML_IMAGE_PATH.$old_image))
			{$error['image'] = "Error deleting old image ";}
			else
			{$error['image'] = 'Deleted old image';}
		}
		
		move_uploaded_file($_FILES["image"]["tmp_name"], HTML_IMAGE_PATH.$new_name);
		$error['image'] = "Stored as: " .$new_name;
		
		$update = 'UPDATE '.DB_PERFIX.'template_images SET image="'.$new_name.'" WHERE id='.$id.' LIMIT 1;';
		$update = mysql_query($update);
	
		return $error['image'];
	}
}
?>